//  Copyright 2009 David Ekholm, Jalbum AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package net.jalbum.editor;

package net.jalbum.jcamerainfo;

import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDescriptor;
import com.drew.metadata.exif.ExifDirectory;
import com.drew.metadata.exif.NikonType2MakernoteDirectory;
import java.awt.BorderLayout;
import java.awt.SystemColor;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import se.datadosen.imaging.exif.ImageInfoFormatter;
import se.datadosen.jalbum.AlbumObject;
import se.datadosen.jalbum.EditListener;
import se.datadosen.jalbum.Msg;
import se.datadosen.jalbum.PluginContext;

public class JCameraInfoPanel extends JPanel implements EditListener {
    private AlbumObject ao;
    private JTextArea content = new JTextArea();
    
    private DecimalFormat twoDec = new DecimalFormat("0.##");
    private ImageInfoFormatter formatter = new ImageInfoFormatter();
    private PluginContext context;
    private boolean showing = false;
    
    public JCameraInfoPanel(PluginContext context) {
        this.context = context;
        init();
    }
    
    /**
     * Print the text contents of this panel
     */
    @Override
    public String toString() {
        if (ao == null) {
            return "";
        }
        Metadata meta = ao.getMetadata().getMetadata();
        formatter.setMetadata(meta);
        StringBuilder buf = new StringBuilder();
        
        buf.append(ao.getName() + "\n");
        
        if (meta != null && meta.containsDirectory(ExifDirectory.class)) {
            Directory dir = meta.getDirectory(ExifDirectory.class);
            if (!dir.containsTag(ExifDirectory.TAG_MODEL)) {
                return buf.toString();
            }
            ExifDescriptor desc = new ExifDescriptor(dir);
            try {
                buf.append(dir.getDescription(ExifDirectory.TAG_DATETIME_ORIGINAL) + "\n");
                buf.append(desc.getDescription(ExifDirectory.TAG_MAKE) + " - " + desc.getDescription(ExifDirectory.TAG_MODEL) + "\n");
                Directory nikon = null;
                if (meta.containsDirectory(NikonType2MakernoteDirectory.class)) {
                    nikon = meta.getDirectory(NikonType2MakernoteDirectory.class);
                }
                
                if (nikon != null) {
                    buf.append(nikon.getDescription(NikonType2MakernoteDirectory.TAG_NIKON_TYPE2_LENS) + "\n");
                }
                buf.append("(" + dir.getInt(ExifDirectory.TAG_EXIF_IMAGE_WIDTH)
                        + " x " + dir.getInt(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT)
                        + ") - " + twoDec.format((float)ao.getFile().length()/1024/1024) + " MB \n");
                buf.append(desc.getFocalLengthDescription());
                String focalLength35mm = (String)formatter.format(ExifDirectory.TAG_35MM_FILM_EQUIV_FOCAL_LENGTH);
                if (focalLength35mm != null) {
                    buf.append(" (∼" + focalLength35mm + ")");
                }
                buf.append('\n');
                buf.append(desc.getExposureModeDescription() + " (" + "ISO " + desc.getIsoEquivalentDescription() + ")\n");
                buf.append(desc.getExposureTimeDescription() + " - " + desc.getFNumberDescription() + "\n");
                buf.append(desc.getExposureBiasDescription() + " - " + dir.getDescription(ExifDirectory.TAG_WHITE_BALANCE_MODE));
                if (nikon != null) {
                    buf.append("(" + nikon.getDescription(NikonType2MakernoteDirectory.TAG_NIKON_TYPE2_CAMERA_WHITE_BALANCE) + ")");
                }
            } catch (MetadataException ex) {
                System.err.println(ex);
                return "Error extracting metadata";
            }
        }
        return buf.toString();
    }
    
    public void setAlbumObject(AlbumObject ao) {
        this.ao = ao;

        
        if (ao == null || (ao.isFolder() && showing)) {
            // Remove panel from UI
            context.getEditPanel().removeCustomTab(this);
            showing = false;
            return;
        }
        else if (!ao.isFolder() && !showing) {
            // Install panel in UI
            context.getEditPanel().addCustomTab(Msg.get("edit.camera"), this);
            showing = true;
        }
        content.setText(toString());
    }

    private void init() {
        setLayout(new BorderLayout());

        //setBackground(new Color(230,255,230));
        content.setOpaque(false);
        content.setFont(new JTextField().getFont());

//        content.setBackground(SystemColor.control);
        content.setForeground(SystemColor.darkGray);
        setBackground(SystemColor.text);
        setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
        content.setFont(content.getFont().deriveFont(11.0f));
        content.setEditable(false);
        content.setToolTipText("<html>"+
                "File name<br>" +
                "Original date<br>" +
                "Camera make - model<br>" +
                "Lens type (if available)<br>" +
                "(Pixel size) - Disk size<br>" +
                "Focal length (35mm equivalent)<br>" +
                "Exposure mode (ISO equivalent)<br>" +
                "Exposure time - FNumber<br>" +
                "Exposure bias - W. bal. mode (Temp)" +
                "</html>"
                );
 
        add(content, BorderLayout.CENTER);
        validate();
    }

}
